//类描述 显示表格及js实体

Ext.define('ProductGroup', {
    extend: 'Ext.data.Model',
    fields: [
        "Id"
		, "CompanyID"   //企业ID
		, "AreaName"   //城市
        , "ProductType"
		, "Product"   //产品
		, "Price"   //价格
		, "ContractCode"   //合同编号
		, "AreaCode"   //城市编号
		, "ProductID"   //产品ID
        , "IsAdd"
        , "AgentName"   //委托方名称
		, "AgentID"   //委托方ID
		, "AgentPrice"   //委托价格
				    ],
    idProperty: "Id"
});

Ext.define('SHApp.ClientInfo.ProductGroup.InfoGrid', {
    extend: 'BLBTSystem.grid',
    alias: 'shapp.clientinfo.grid',
    requires: ['SHApp.System.Maintenance.MaintenComBox',
    'SHApp.System.AdministrativeArea.AreaCmbTree'],
    txtAreasID: 'txtAreas',
    txtProductID: 'txtProduct',
    dataModel: "ProductGroup",
    dataUrl: "api/productgroup?format=json",
    isAdd: false, //换成可编辑的
    hasEditCell: false,
    autoSync: false,
    code: "",
    contractCode: "",
    companyID: "",
    initComponent: function () {
        var me = this;
        Ext.apply(this);
        me.btnAddID += me.id;
        me.btnEditID += me.id;
        me.btnDeleteID += me.id;
        me.btnSaveID += me.id;
        me.btnResetID += me.id;
        me.txtAreasID += me.id;
        me.txtProductID += me.id;
        this.creatTbar();
        this.creatColumns();
        this.callParent(arguments);
        this.on("beforeedit", function (editor, e, op) {
            if (!me.isAdd) {
                return false;
            }
        });
        this.checkBtnPermission(me.pvalue);
    },
    creatColumns: function () {
        var me = this;
        me.columns = [{
            text: "Id",
            flex: 1,
            dataIndex: 'Id',
            sortable: false,
            hidden: true
        }, {
            text: "合同包编号",
            width: 100,
            dataIndex: 'Code',
            sortable: true,
            hidden: true
        }, {
            text: "企业ID",
            width: 100,
            dataIndex: 'CompanyID',
            sortable: true,
            hidden: true
        }, {
            text: "城市",
            width: 100,
            dataIndex: 'AreaName',
            sortable: true
        }, {
            text: "产品类型",
            width: 100,
            dataIndex: 'ProductType',
            sortable: true
        }, {
            text: "产品",
            width: 100,
            dataIndex: 'Product',
            sortable: true
        }, {
            text: "价格",
            width: 100,
            dataIndex: 'Price',
            field: {
                xtype: "numberfield",
                minValue: 0
            }
        }, {
            text: "合同编号",
            width: 100,
            dataIndex: 'ContractCode',
            sortable: true,
            hidden: true
        }, {
            text: "城市编号",
            width: 100,
            dataIndex: 'AreaCode',
            sortable: true,
            hidden: true
        }, {
            text: "产品ID",
            width: 100,
            dataIndex: 'ProductID',
            sortable: true,
            hidden: true
        }, {
            text: "委托方名称",
            width: 100,
            dataIndex: 'AgentName',
            sortable: true
        }, {
            text: "委托方ID",
            width: 100,
            dataIndex: 'AgentID',
            sortable: true,
            hidden: true
        }, {
            text: "委托方价格",
            width: 100,
            dataIndex: 'AgentPrice',
            sortable: true
        }
		]
    },
    loadByUserID: function (contractCode, companyID) {
        var me = this;
        me.isAdd = false;
        this.down("#" + this.btnSaveID).setDisabled(false);
        this.down("#" + this.btnResetID).setDisabled(false);
        me.contractCode = contractCode;
        me.companyID = companyID;

        me.clearFilters();
        if (contractCode && contractCode.length > 0) {
            me.addFilter("ContractCode", contractCode);
        }
        //        if (companyID && companyID.length > 0) {
        //            me.addFilter("CompanyID", companyID);
        //        }
        me.store.proxy.url = me.dataUrl + "&isAdd=" + me.isAdd + "&ContractCode=" + me.contractCode;
        me.loadByFilter();
    },
    creatTbar: function () {
        var me = this;
        me.tbar = [
			{
			    xtype: 'areacmbtree',
			    disabled: false,
			    fieldLabel: '地区:',
			    labelWidth: 40,
			    name: 'AreaCode',
			    id: me.txtAreasID,
			    width: 130
			}, {
			    xtype: 'button',
			    text: '查询',
			    //id: me.btnAddID,
			    iconCls: 'icon-btn-search',
			    handler: function () {
			        //var me = this;
			        me.isAdd = true;
			        if (me.contractCode == "" || me.companyID == "") {
			            Ext.Msg.alert("提示", "请先选择合同包基本信息！");
			            return;
			        }

			        me.clearFilters();
			        var vAreas = me.down("#" + me.txtAreasID).getValue();
			        if (vAreas && vAreas.length > 0) {
			            me.addFilter("AreaCode", vAreas);
			        }
			        me.store.proxy.url = me.dataUrl + "&isAdd=" + me.isAdd + "&ContractCode=" + me.contractCode + "&CompanyID=" + me.companyID;
			        me.loadByFilter();
			    }
			}, '->', {
			    xtype: 'button',
			    disabled: true,
			    id: me.btnSaveID,
			    text: '保存',
			    iconCls: 'icon-btn-save',
			    handler: function () {
			        me.store.update();
			        Ext.Msg.alert("提示信息", "保存成功！");
			    }
			}, {
			    xtype: 'button',
			    disabled: true,
			    id: me.btnResetID,
			    text: '重置',
			    iconCls: 'icon-btn-reset',
			    handler: function () {
			        me.store.load();
			    }
			}
        ]
    }
})

